﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPDataServiceDemo;
using WPDataServiceDemo.Models;
using WPDataServiceDemo.libs;

namespace WPDataServiceDemo.Queries
{
    public  class GetData
    {
        public static WPServiceModel GetDb()
        {
            try
            {
                WPServiceModel db = new WPServiceModel();
                return db;
            }
            catch (Exception ex)
            {
                throw new Exception("Xin lỗi, vui lòng thử lại, bạn chưa kết nối với database!");
                return null;
            }
         }

        public static IEnumerable<supplier> getListSupplier()
        {
            try
            {
                var factory = GetData.GetDb();
                var data = factory.suppliers;
                return data;
             }
            catch (Exception ex)
            {
                return null;
            }
        }


        public static IEnumerable<supplier> searchSupplierByName(String keySearch)
        {
           
                var factory = GetData.GetDb();
                var data = factory.suppliers.Where(s => s.name.Contains(keySearch));
                return data;
          
        }

        public static IEnumerable<menu> SearchMenuByAddressAndName(String menuName, String AddressName)
        {
            menuName        =  StringLibs.locDau(menuName);
            AddressName     =  StringLibs.locDau(AddressName);
                var factory = GetData.GetDb();
                var data = from mn in factory.menus
                           where  mn.name.Contains(menuName)
                           where  mn.supplier.address.Contains(AddressName)
                           select mn ;

                return data;
           
        }

        public static IEnumerable<menu> getMenusByListId(int[] ids)
        {
            var factory = GetData.GetDb();
            var data = from mn in factory.menus
                      where ids.Contains(mn.id)
                      select mn;

            return data;

        }

        public static supplier getSupplierById(int id)
        {
            try
            {
                var factory = GetData.GetDb();
                var data = factory.suppliers.Single(s => s.id == id);
                return data;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

      

        public static IEnumerable<menu> getListMenuBySupplierId(int id)
        {
            try
            {
                var data = GetData.getSupplierById(id).menus;
                return data;
            }
            catch (Exception ex)
            {
                return null;
            }
        }


        public static IEnumerable<menu> searchMenuByName(String keySearch)
        {
            try
            {
                var factory = GetData.GetDb();
                var data = factory.menus.Where(m => m.name.Contains(keySearch));
                return data;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static menu getMenuById(int id)
        {
            try
            {
                var factory = GetData.GetDb();
                var data = factory.menus.Single(m => m.id == id);
                return data;
            }
            catch (Exception ex)
            {
                return null;
            }
        }


        public static void Booking(order order, IEnumerable<order_detail> orderDetails)
        {
            try
            {
                var factory = GetData.GetDb();
                factory.AddToorders(order);
                factory.SaveChanges();
                foreach (order_detail orderDetail in orderDetails)
                {
                    orderDetail.order = order;
                }
                factory.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
         }
    

    }
}
